<?php
/**
 * File: DefaultController.php.
 * User: Yan<me@xiaoyan.me>
 * DateTime: 2017-12-20 9:08
 */

namespace App\Controller;


use App\Core\Request;
use App\Core\WebController;
use App\Models\VoteInfo;
use App\Models\VoteProject;

class DefaultController extends WebController
{
    public function index(Request $request)
    {
        $vote = new VoteInfo();

        $project_id = 0;
        if ($request->getInt('project_id') > 0) {
            $project_id = $request->getInt('project_id');
        }
        $condition = ['state[!]' => 0];
        $allow_count = 0;
        if ($project_id > 0) {
            $vp = new VoteProject();
            $vp = $vp->find($project_id);
            $this->assign('page_title', $vp['project_name']);
            $condition['project_id'] = $project_id;
            if($request->user){
                $allow_count = json_decode($vp['allow_vote_count'],1);
                $allow_count = $allow_count[$request->user->user_role];
                $allow_count = $allow_count == 0 ? 99999 : $allow_count;
            }
        } else {
            $this->assign('page_title', '所有点赞作品');
        }
        $this->assign('vote_list', $vote->select('*', [
            'AND' => $condition,
            'ORDER' => ['vote_count' => 'DESC']
        ]));
        $this->assign('allow_count',$allow_count);
        $this->assign('nav_project',$project_id);
        $this->render('index');
    }
}